package s2elinshu.swing;


import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class FindZipcode extends JDialog {


	private final JPanel contentPanel = new JPanel();
	private JTextField dong;
	private JTextField etcAddr;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			FindZipcode dialog = new FindZipcode(null);
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public FindZipcode( final JoinMember parent) {
		super(parent, ModalityType.APPLICATION_MODAL);
		setTitle("우편번호 검색");
		setBounds(100, 100, 450, 300);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		JLabel hdr2 = new JLabel("동 이름");
		dong = new JTextField();
		dong.setColumns(10);
		JButton findBtn = new JButton("주소검색");
		
		// 콤보박스 목록 생성 - ArrayList
		ArrayList<String> items = new ArrayList<>();
		items.add("123-456 서울시 구로구 구로동");
		items.add("456-789 서울시 강남구 대치동");
		items.add("789-123 서울시 강남구 역삼동");
		
		// 콤보박스 목록 지정 - 생성자 사용
		final JComboBox addr = new JComboBox( items.toArray() );			// ArrayList를 toArray (배열 형태로 변환) 해서 사용
		
		etcAddr = new JTextField();
		etcAddr.setColumns(10);
		JLabel hdr1 = new JLabel("<html>\r\n<pre>\r\n동, 읍, 면의 이름을 입력하신 후 '우편번호 찾기'를 클릭하세요.\r\n              (예 : 서초동, 우포읍, 외서면)\r\n</pre>\r\n</html>");
		GroupLayout gl_contentPanel = new GroupLayout(contentPanel);
		gl_contentPanel.setHorizontalGroup(
			gl_contentPanel.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_contentPanel.createSequentialGroup()
					.addContainerGap(32, Short.MAX_VALUE)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING, false)
						.addComponent(etcAddr, Alignment.TRAILING)
						.addComponent(addr, Alignment.TRAILING, 0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
						.addGroup(gl_contentPanel.createSequentialGroup()
							.addComponent(hdr2)
							.addGap(18)
							.addComponent(dong, GroupLayout.DEFAULT_SIZE, 209, Short.MAX_VALUE)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(findBtn, GroupLayout.PREFERRED_SIZE, 92, GroupLayout.PREFERRED_SIZE))
						.addComponent(hdr1, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
					.addGap(26))
		);
		gl_contentPanel.setVerticalGroup(
			gl_contentPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(Alignment.TRAILING, gl_contentPanel.createSequentialGroup()
					.addContainerGap(27, Short.MAX_VALUE)
					.addComponent(hdr1)
					.addGap(18)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(hdr2)
						.addComponent(dong, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(findBtn))
					.addGap(18)
					.addComponent(addr, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
					.addGap(18)
					.addComponent(etcAddr, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
					.addGap(24))
		);
		contentPanel.setLayout(gl_contentPanel);
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.CENTER));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("   확인   ");
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						// FindZipcode에서 선택한 주소정보를 JoinMember로 보내기 위해 sendZipcode() 호출
						parent.sendZipcode(addr.getSelectedItem().toString());
						dispose();
					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("   취소   ");
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
	}

}
